'''
Description: If you can force your heart and nerve and sinew. To serve your turn long after they are gone, And so hold on when there is nothing in you. Except the Will which says to them: ‘Hold on!’  
Author: CBen
Date: 2025-06-19 12:18:57
LastEditors: Kipling​
LastEditTime: 2025-07-05 14:01:36
'''
import re
import matplotlib
# 使用非交互后端，确保在无显示器的环境下也能输出文件
matplotlib.use('Agg')
import matplotlib.pyplot as plt

# 正则模式
pattern_shape = re.compile(r"M=(\d+), N=(\d+), K=(\d+)")
pattern_tflops = re.compile(r"([\d\.]+)\s+TFLOPS")

shapes = []
tflops = []

# 读取你的日志文件，如果不是 log.txt，请改成实际文件名
with open("./log/run_test_sync.log", "r", encoding="utf-8") as f:
    lines = f.readlines()

# 提取数据
for idx, line in enumerate(lines):
    m = pattern_shape.search(line)
    if m:
        shapes.append(f"{m.group(1)}x{m.group(2)}x{m.group(3)}")
        for later in lines[idx+1:]:
            p = pattern_tflops.search(later)
            if p:
                tflops.append(float(p.group(1)))
                break

# X 轴索引
x = list(range(len(tflops)))

# —— 折线图 —— #
plt.figure(figsize=(20, 6))
plt.plot(x, tflops, linestyle='-', linewidth=1)
plt.xlabel("Test Index")
plt.ylabel("Performance (TFLOPS)")
plt.title("GEMM Performance (Line)")
# 隐藏 X 轴标签文字，仅留刻度线
plt.tick_params(axis='x', labelbottom=False, bottom=True)
plt.grid(True)
plt.tight_layout()
# 保存到文件
plt.savefig("./pic/gemm_line.png")
plt.close()

# —— 柱状图 —— #
plt.figure(figsize=(20, 6))
plt.bar(x, tflops, width=1.0)
plt.xlabel("Test Index")
plt.ylabel("Performance (TFLOPS)")
plt.title("GEMM Performance (Bar)")
plt.tick_params(axis='x', labelbottom=False, bottom=True)
plt.grid(axis='y')
plt.tight_layout()
plt.savefig("./pic/gemm_bar_sync.png")
plt.close()

print("Generated: gemm_line.png, gemm_bar.png")
